package dshop.model.commission;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;

import appbase.jfinal.ext.BaseModel;
import appbase.kit.encrypt.MD5Kit;

public class Commission extends BaseModel<Commission>{
	
	private static final long serialVersionUID = -6967680902549117464L;
	
	public static final Commission dao = new Commission();

	//推广获得积分
	public static final int CHANGE_TYPE_COMMISSION_EVENT = 1;

	//101 积分提现；102 提现失败，返还积分
	public static final int CHANGE_TYPE_DRAW_CASH = 101;
	public static final int CHANGE_TYPE_DRAW_CASH_FAIL_RETURN = 102;
	
	public static Map<Integer, String> changeTypeMap()
	{
		Map<Integer, String> map = new HashMap<Integer, String>();
		map.put(CHANGE_TYPE_COMMISSION_EVENT, "推广获得积分");
		map.put(CHANGE_TYPE_DRAW_CASH, "积分提现");
		map.put(CHANGE_TYPE_DRAW_CASH_FAIL_RETURN, "提现失败，返还积分");
		return map;
	}

	public static void onPlus(int userId, BigDecimal money, String descs, int changeType, int changeRefId) 
	{
		if (money == null || money.compareTo(BigDecimal.ZERO) <= 0)
        {
            throw new RuntimeException("金额不能小于0");
        }

        Commission balance = getCommission(userId);

        BigDecimal plusMoney = money.setScale(2, RoundingMode.DOWN);
        BigDecimal moneyBefore = balance.getBigDecimal("money").setScale(2, RoundingMode.DOWN);
        BigDecimal moneyAfter = moneyBefore.add(plusMoney).setScale(2, RoundingMode.DOWN);

        CommissionDetail detail = CommissionDetail.createCommissionDetail(userId, plusMoney, moneyBefore, moneyAfter, descs, changeType, changeRefId);
        detail.save();

        balance.set("money", moneyAfter);
        balance.set("version", balance.getInt("version") + 1);

        String sign = generateSign(balance.getInt("id"), balance.getInt("user_id"), balance.getBigDecimal("money"), balance.getInt("version"));
        balance.set("sign", sign);
        
        balance.update();
	}

	private static Commission getCommission(int userId) {
		Commission commission = dao.findFirstByColumns(" * ", "user_id", userId);
		if (commission == null){
            commission = initCommission(userId);
        }
        return commission;
	}

	private static Commission initCommission(int userId) {
		BigDecimal money = new BigDecimal("0.00").setScale(2);
        Commission commission = createCommission(userId, money);

        commission.save();

        String sign = generateSign(commission.getInt("id"), commission.getInt("user_id"), commission.getBigDecimal("money"), commission.getInt("version"));
        dao.updateColumnBy("sign", "id", sign, commission.getInt("id"));

        return commission;
	}

	private static Commission createCommission(int userId, BigDecimal money) {
        Commission commission = new Commission();
        commission.set("user_id", userId);
        commission.set("money", money);
        commission.set("sign", "");
        commission.set("version", 0);
        return commission;
    }
	
	
	private static String generateSign(int id, int userId, BigDecimal money, int version) {
        StringBuilder signStrBuilder = new StringBuilder();
        signStrBuilder.append(id).append("_3XOS_").append(money).append("_eiwe_").append(userId).append("_").append("RDSL").append(version);
        return MD5Kit.encrptMD5(signStrBuilder.toString());
    }
	
	
	public static boolean checkSign(int id, int userId, BigDecimal money, int version, String sign)
	{
		String signNew = generateSign(id, userId, money, version);
		return signNew.equals(sign);
	}
}
